/**
 * @description 路由配置
 */

import Vue from 'vue'
import Router from 'vue-router'
import App from '@/components/App'
import NotFoundPage from '@/components/NotFoundPage'
import { login, register } from './login.register'
import operMgmtRoute from './operMgmt.module'
import userCenterRoute from './userCenter.module'
import systemRoute from './system.module'
import monitorAlarmRoute from './monitorAlarm.module'
import consoleModule from './console.module'

const routes = [
  login,
  register,
  {
    path: '/',
    component: App,
    children: [{ path: '/not-found', component: NotFoundPage }]
      .concat(operMgmtRoute)
      .concat(userCenterRoute)
      .concat(systemRoute)
      .concat(monitorAlarmRoute)
      .concat(consoleModule)
  }
]

Vue.use(Router)

const router = new Router({
  routes: routes
})

router.beforeEach((to, from, next) => {
  if (to.name === 'login' || to.name === 'register') {
    next()
  } else {
    sessionStorage.getItem('vi-login')
      ? to.matched.length
        ? next()
        : next({ path: '/not-found' })
      : next({ path: '/login' })
  }
})
export default router
